//获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

function getAttackName(num) {
    switch (num)
    {
        case "1":
            x = "ICMP Redirection";
            break;
        case "2":
            x = "ICMP Unreacheble";
            break;
        case "3":
            x = "TCP anomaly Connection";
            break;
        case "4":
            x = "DNS Query Application Attack";
            break;
        case "5":
            x = "DNS Reply Application Attack";
            break;
        case "6":
            x = "SIP Application Attack";
            break;
        case "7":
            x = "HTTPS Application Attack";
            break;
        case "8":
            x = "HTTP GET Application Attack";
            break;
        case "9":
            x = "Challenge Collapsar Attack";
    }
    return x;
}

function showData() {
//    overlayer("open");
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/get.do",
        data: {type: type, messageNo: id, instance: DepotName},
        dataType: "json",
        success: function(souData) {
            //souData = $.parseJSON(souData);
            var policy_desc = souData.Comment;
            $(".policy_desc").val(policy_desc);
            var data = souData.Content;
            var lis = data.APP_Attack_Types.APP_Attack;
            var len = lis.length;
            var straStr = "";
            $("#list").html("");
            for (var i = 0; i < len; i++) {
                var attack_type = lis[i].APP_Attack_Type;
//                var attack_name = lis[i].APP_Attack_Type;
                var attack_name = getAttackName(attack_type);
                var attack_threshold = lis[i].Attack_Threshold;
                var control_threshold = lis[i].Attack_Control;
                straStr += '<li class="lis" type="' + attack_type + '"><span>' + attack_name + ',' + attack_threshold + ',' + control_threshold + '</span><button class="btn btn-link" type="button" onclick="delStrategy(this)"><i class="icon icon-remove red"></i></button></li>';
            }
            $("#list").append(straStr);
        }
    });
}

//获取表单数据
function generateParameter() {
    var lis = $("ul#list").find("li");
    var len = lis.length;
    var obj = {};
    obj.APP_Attack_Types = {};
    obj.APP_Attack_Types.APP_Attack = [];
    obj.MessageSerialNo = "0";
    if (len == 0) {
        alert("还未添加策略！");
    } else {
        $.each(lis, function(index, item) {
//            console.log(item);
            var type = $(item).attr("type");
            var ali = $(item).children("span").html();
            var ali_arr = ali.split(",");
            var tmp = {};
            tmp.APP_Attack_Type = type;
            tmp.Attack_Threshold = ali_arr[1];
            tmp.Attack_Control = ali_arr[2];
            obj.APP_Attack_Types.APP_Attack.push(tmp);

        });
    }

    return obj;
}

//添加数据
function save() {
    var comment = $(".policy_desc").val();
    if (comment == "") {
        alert("策略名称不能为空！");
        return;
    }
    var policy = JSON.stringify(generateParameter());
//    console.log(policy);
//    return;
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/create.do",
        data: {comment: comment, policy: policy, type: type, instance: DepotName},
        dataType: "json",
        success: function(data) {
            if (data.result == "true") {
                alert("添加成功！");
                previous();
            } else {
                alert("添加失败!");
            }
        }
    });
}

//编辑数据
function edit() {
//    var form = $(".form-horizontal").validate();
//    if (!form.form()) {
//        return;
//    }
    var comment = $(".policy_desc").val();
    if (comment == "") {
        alert("策略名称不能为空！");
        return;
    }
    var policy = JSON.stringify(generateParameter());
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/update.do",
        data: {messageNo: id, comment: comment, policy: policy, type: type, instance: DepotName},
        dataType: "json",
        success: function(data) {
            if (data.result == "true") {
                alert("编辑成功！");
                previous();
            } else {
                alert("编辑失败!")
            }
        }
    });
}

//返回上一级
function previous() {
    window.location = "../policyList.html?type=" + type + "&id=" + DepotID + "&name=" + base64.encode(DepotName) + "";
}

$(function() {
    setInterval(function() {
        parent.$("#page-content").attr("height", $("html").height());
    }, 500);
});
/*
 * 查看旧版本的策略内容
 */
function show_oldPolicy(array, number) {
    var souData = array[number];
    $(".header").text("当前策略在" + souData.CreateTime + "创建,ID为" + souData.MessageNo + ",版本号为" + souData.MessageSerialNo + ",属于" + souData.Instance + "策略库");

    var policy_desc = souData.Comment;
    $(".policy_desc").val(policy_desc);
    var data = souData.Content;
    var lis = data.APP_Attack_Types.APP_Attack;
    var len = lis.length;
    var straStr = "";
    $("#list").html("");
    for (var i = 0; i < len; i++) {
        var attack_type = lis[i].APP_Attack_Type;
//                var attack_name = lis[i].APP_Attack_Type;
        var attack_name = getAttackName(attack_type);
        var attack_threshold = lis[i].Attack_Threshold;
        var control_threshold = lis[i].Attack_Control;
        straStr += '<li class="lis" type="' + attack_type + '"><span>' + attack_name + ',' + attack_threshold + ',' + control_threshold + '</span><button class="btn btn-link" type="button" onclick="delStrategy(this)"><i class="icon icon-remove red"></i></button></li>';
    }
    $("#list").append(straStr);
}

/*
 *获取所有版本的策略内容 
 */
function getAllPolicy() {
    $.ajax({
        type: "GET",
        async: false,
        url: "../../action/policy/history_policy_info.do",
        data: {type: type, instance: getQueryString("DepotName"), messageNo: id},
        dataType: "json",
        success: function(data) {
            policyArray = data;
            policyAmount = data.length
        }
    });
}
